﻿@inherits TestContext

@code 
{
    private readonly Fixture _fixture = new();

    public FavoriteButtonTests()
    {
        this.AddBlazoredLocalStorage();
        this.Services.AddScoped<AppState>();
    }

    [Fact]
    public void RendersAddFavoriteButton_When_TrailIsNotFavorited()
    {
        // Arrange
        var testTrail = _fixture.Create<Trail>();

        // Act
        var cut = Render(@<FavoriteButton Trail="testTrail" />);

        // Assert
        cut.MarkupMatches(
            @<button class="btn btn-outline-primary ml-1" title="Favorite">
                <i class="bi bi-heart"></i>
            </button>
        );
    }

    [Fact]
    public async Task RendersRemoveFavoriteButton_When_TrailIsFavorited()
    {
        // Arrange
        var testTrail = _fixture.Create<Trail>();

        var appState = this.Services.GetService<AppState>();
        await appState.FavoriteTrailsState.AddFavorite(testTrail);

        // Act
        var cut = Render(@<FavoriteButton Trail="testTrail" />);

        // Assert
        cut.MarkupMatches(
            @<button class="btn btn-outline-primary ml-1" title="Favorite">
                <i class="bi bi-heart-fill"></i>
            </button>
        );
    }

    [Fact]
    public void AddTrailToFavorites_When_TrailIsNotAFavorite()
    {
        // Arrange
        var testTrail = _fixture.Create<Trail>();
        var cut = Render(@<FavoriteButton Trail="testTrail" />);
        var button = cut.Find("button");

        //Act
        button.Click();

        // Assert
        var appState = this.Services.GetService<AppState>();
        Assert.True(appState.FavoriteTrailsState.IsFavorite(testTrail));
    }

    [Fact]
    public async Task RemoveTrailFromFavorites_When_TrailIsFavorite()
    {
        // Arrange
        var testTrail = _fixture.Create<Trail>();
        var appState = this.Services.GetService<AppState>();
        await appState.FavoriteTrailsState.AddFavorite(testTrail);
        var cut = Render(@<FavoriteButton Trail="testTrail" />);
        var button = cut.Find("button");

        //Act
        button.Click();

        // Assert
        Assert.False(appState.FavoriteTrailsState.IsFavorite(testTrail));
    }
}
